/**
 * 消息框,错误框,确认框,等待框等封装
 */
import base from '@/common/sju.base.js';
let alert = {
	/**
	 * @description 提示消息,一会就自动消失
	 * @param {string} msg 要显示的消息
	 * @param {number} second 显示时间(毫秒,默认1000毫秒)
	 */
	showInfo: function (msg, second) {
		if (base.isNull(second))
			second = 1000;
		uni.showToast({
			title: msg,
			icon: 'none',
			duration: second
		});
	},
	/**
	 * @description 提示错误消息,需要点击确认后关闭
	 * @param {string} msg 错误消息
	 * @param {type} title 错误标题[默认'提示']
	 */
	showError: function (msg, title) {
		if (base.isNull(title))
			title = "提示";
		uni.showModal({
			title: title,
			content: msg,
			showCancel: false
		});
	},
	/**
	 * @description 显示提示,确认后进行页面跳转
	 * @param {string} msg 错误消息
	 * @param {type} url 跳转地址
	 * @param {type} title 错误标题[默认'提示']
	 */
	showAndRedirect: function (msg, url, title) {
		if (base.isNull(title))
			title = "提示";
		uni.showModal({
			title: title,
			content: msg,
			showCancel: false,
			success: function () {
				console.log(url)
				uni.redirectTo({ url: url });
			}
		});
	},
	/**
	 * @description 提示确认框,需要点击确认后关闭
	 * @param {string} msg 消息
	 * @param {function} confirm 点击确认后执行的回调
	 * @param {function} cancel 点击取消后执行的回调
	 * @param {string} title 标题[默认'请确认']
	 */
	showConfirm: function (msg, confirm, cancel, title) {
		if (base.isNull(title))
			title = "请确认";
		uni.showModal({
			title: title,
			content: msg,
			success: function (e) {
				if (e.confirm == true)
					confirm();
				else
					cancel();
			}
		});
	},
	/**
 * @description 显示带输入框的确认框
 * @param {Object} options 配置项
 * @param {string} options.title 标题
 * @param {string} options.content 内容
 * @param {string} options.placeholderText 输入框占位符
 * @param {string} options.confirmText 确认按钮文本
 * @param {string} options.cancelText 取消按钮文本
 * @returns {Promise} 返回Promise，resolve({confirm, content})
 */
	showInput: function (options) {
		return new Promise((resolve) => {
			uni.showModal({
				title: options.title || '请输入',
				content: options.content || '',
				editable: true,
				placeholderText: options.placeholderText || '',
				confirmText: options.confirmText || '确认',
				cancelText: options.cancelText || '取消',
				success: (res) => {
					resolve({
						confirm: res.confirm,
						content: res.content
					});
				}
			});
		});
	},
	/**
	 * @description 显示等待框
	 */
	showLoading: function () {
		uni.showLoading({
			title: '请等待',
			mask: true
		});
	},
	/**
	 * @description 隐藏等待框
	 */
	hideLoading: function () {
		uni.hideLoading();
	}
}

export default alert;
